using Microsoft.EntityFrameworkCore;
using WpfClient.Core.Models;

namespace WpfClient.Infrastructure.Data
{
    /// <summary>
    /// 应用程序数据库上下文，与EF Core相关
    /// </summary>
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            // 配置User实体
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Email).IsRequired().HasMaxLength(200);
                entity.HasIndex(e => e.Email).IsUnique();
                entity.Property(e => e.IsActive).HasDefaultValue(true);
                entity.Property(e => e.CreatedAt).HasDefaultValueSql("datetime('now')");
            });

            // 添加种子数据
            modelBuilder.Entity<User>().HasData(
                new User { Id = 1, Name = "管理员", Email = "admin@example.com", IsActive = true, CreatedAt = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc) },
                new User { Id = 2, Name = "测试用户", Email = "test@example.com", IsActive = true, CreatedAt = new DateTime(2024, 1, 2, 0, 0, 0, DateTimeKind.Utc) },
                new User { Id = 3, Name = "张三", Email = "zhangsan@example.com", IsActive = true, CreatedAt = new DateTime(2024, 1, 3, 0, 0, 0, DateTimeKind.Utc) },
                new User { Id = 4, Name = "李四", Email = "lisi@example.com", IsActive = false, CreatedAt = new DateTime(2024, 1, 4, 0, 0, 0, DateTimeKind.Utc) },
                new User { Id = 5, Name = "王五", Email = "wangwu@example.com", IsActive = true, CreatedAt = new DateTime(2024, 1, 5, 0, 0, 0, DateTimeKind.Utc) }
            );
        }
    }
}